//
//  UIView+codeLayout.m
//  YiYueStore
//
//  Created by zhouzhouzhou on 2018/10/24.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "UIView+codeLayout.h"

@implementation UIView (codeLayout)

-(void)setViewSectionInset:(UIEdgeInsets)sectionInset superView:(UIView *)superView{
    
    //子view的上边缘离父view的上边缘40个像素
    NSLayoutConstraint *contraint1 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1.0 constant:sectionInset.top];
    //子view的左边缘离父view的左边缘40个像素
    NSLayoutConstraint *contraint2 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:sectionInset.left];
    //子view的下边缘离父view的下边缘40个像素
    NSLayoutConstraint *contraint3 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-sectionInset.bottom];
    //子view的右边缘离父view的右边缘40个像素
    NSLayoutConstraint *contraint4 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-sectionInset.right];
    
    //把约束添加到父视图上
    NSArray *array = [NSArray arrayWithObjects:contraint1, contraint2, contraint3, contraint4, nil];
    [superView addConstraints:array];
}

@end
